package jmine.tec.security.ldap;

import java.util.ArrayList;
import java.util.List;

import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.AuthenticationErrorCallback;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.LdapTemplate;

/**
 * Mock para uso em testes e/ou login sem uma base LDAP no ambiente de desenvolvimento.
 * 
 * @author seiti
 * @created Sep 20, 2010
 */
public class MockLdapTemplate extends LdapTemplate {

    private List<String> groups;

    /**
     * Ctor
     * 
     * @param contextSource
     */
    public MockLdapTemplate(ContextSource contextSource) {
        super(contextSource);
    }

    /**
     * {@inheritDoc}
     */
    public void setGroups(List<String> groups) {
        this.groups = groups;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean authenticate(String base, String filter, String password) {
        return filter.toLowerCase().contains("uid=a") && password.toLowerCase().equals("a");
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean authenticate(String base, String filter, String password, AuthenticationErrorCallback errorCallback) {
        return filter.toLowerCase().contains("uid=a") && password.toLowerCase().equals("a");
    }
    
    /**
     * Lista os grupos ao qual o usuário está associado no LDAP.
     */
    @Override
    public List search(String base, String filter, AttributesMapper mapper) {
        if (!filter.toLowerCase().contains("uid=a")) {
            return new ArrayList<String>();
        }
        return this.groups;
    }
}
